package controller;

import java.util.ArrayList;
import java.util.List;

import model.generate.SvnProject;
import model.generate.SyncProject;
import model.vo.MessageResult;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import service.back.SvnProjectBackService;
import service.back.SyncProjectBackService;
import utils.SVNUtils;
import utils.StringUtils;

@Controller
@Scope("prototype")
@RequestMapping("/SvnClientController")
public class SvnClientController extends BaseController {

	private SvnProjectBackService svnService=new SvnProjectBackService();
	private SyncProjectBackService syncService=new SyncProjectBackService();
	// svn_返回页面
	@RequestMapping("/svn_page.do")
	public String svn_page() {
		return "svn.html";
	}
	@RequestMapping("/svn_sync.do")
	@ResponseBody
	public void svn_syncProject(Integer id)
	{
		MessageResult result=null;
		SvnProject svnProject=svnService.getByID(id);
		List<SyncProject> syncProjects=null;
		if(svnProject!=null)
		{
			String basepath=svnProject.getBasepath();
			String username=svnProject.getUsername();
			String password=svnProject.getPassword();
			syncProjects=syncService.findSyncProjectsByProjectId(id);
			if(syncProjects==null||syncProjects.size()==0)
			{
				result.success=false;
				result.message="没有可同步的数据";
				writeJsonToPage(result);
				return;
			}
			boolean rs=SVNUtils.init(basepath, username, password);
			if(rs)
			{
				result=doSync(svnProject,syncProjects);
			}
			else
			{
				result=new MessageResult(false, "");
				result.success=false;
				result.message="链接不上svn";
			}
		}
		else
		{
			result=new MessageResult(false, "");
			result.success=false;
			result.message="没有查询到对应的项目";
		}
		writeJsonToPage(result);
	}
	/*
	 * 同步
	 */
	private MessageResult doSync(SvnProject svnProject,List<SyncProject> syncProjects)
	{
		MessageResult rs=new MessageResult(true, "");
		String localpath=svnProject.getLocalpath();
		ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
		List<SVNURL> copyUrlList=new ArrayList<SVNURL>();
		List<SVNURL> delUrlList=new ArrayList<SVNURL>();
		String dest="";
		String source="";
		for(int i=0;i<syncProjects.size();i++)
		{
			SyncProject project=syncProjects.get(i);
			
			source=project.getSource();
			if(source.indexOf("svn")<0)
			{
			  source=svnProject.getBasepath()+"/"+source;
			}
			
			dest=project.getDest();
			if(dest.indexOf("svn")<0)
			{
			  dest=svnProject.getBasepath()+"/"+dest;
			}
			SVNURL copyURL;
			SVNURL delURL;
			try {
				copyURL= SVNURL.parseURIEncoded(source);
				copyUrlList.add(copyURL);
				delURL = SVNURL.parseURIEncoded(dest);
				delUrlList.add(delURL);
				
			} catch (SVNException e) {
				rs.success=false;
				rs.message=e.getMessage();
			}
			
			
		}
		List<String> arr=SVNUtils.CopyAndUpdateSvn(localpath,delUrlList, copyUrlList);
		rs.message=StringUtils.join(arr, "\n");
		return rs;
	}
}
